<?php
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

use Think\Controller;
use Common\Controller\CommonActions\OrgnizerActionController as OrgC;

/**
 * 后台首页控制器
 */
class IndexController extends AdminBaseController{


    /**
     * 后台首页框架
     */
	public function index()
    {
		$this->display();
	}

    /**
     * 显示测评组织者管理面板
     */
    public function showOrgnizersInfo()
    {
        $this->assign('rs', OrgC::showOrgnizersInfo());
        $this->display();
    }


    public function getAllUserList(){
        $querydata = I("searchkey");
        $users = M('users');

        if($querydata['classinfo'] == 'all'){

        }
        else{
            $data = $users->select();

            $this->ajaxReturn($data);

        }
    }


    public function getNormalUserReportList()
    {
        $data = M()->query("select users.uid, users.username, users.email from users, calc_result where users.uid = calc_result.user_id and users.oid = 0 GROUP BY users.uid; ");
        $this->ajaxReturn($data);
    }

    public function AdminList()
    {
        if (IS_AJAX){
            $admins = M('admins')->select();

            $this->ajaxReturn($admins);
        }
        else{
            $this->display();
        }

        //var_dump($admins);
    }

    public function addNewAdmin()
    {
        if(IS_AJAX){
            $data['username'] = I('username');
            $data['password'] = I('passwd');

            $data['password'] = md5($data['password']);

            $admin = M('admins');


            $validate = array(
                array('username','require','用户名不能为空！',1),
                array('username','','用户已经存在',0,'unique',1),
            );


            $admin->setProperty("_validate",$validate);

            if ($admin->create($data)){

                if ($admin->add($data)){
                    $rs['msg'] = '新增管理员用户成功！';
                }
            }
            else{
                $rs['msg'] = $admin->getError();
            }
            $this->ajaxReturn($rs);
        }
    }

    public function uploadQuiz()
    {

        $rs = M("quiz_info")->select();
        $this->assign("rs", $rs);

        $this->display();
    }

    public function testUpload()
    {

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('xlsx', 'xls');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息

            $rs['msg'] = $upload->getError();
            $rs['code'] = -2;

        }else{// 上传成功

            $filepath = $_SERVER['DOCUMENT_ROOT'].'/Uploads/'.$info['Filedata']['savepath'].$info['Filedata']['savename'];

            $url = "http://localhost:8090/demo/Main";
            $data['action']='quizcheck';
            $data['filepath'] = $filepath;

            $httpstr = http($url, $data, 'GET', array("Content-type: text/html; charset=utf-8"));//调用问卷部分java

            if (!$httpstr){
                $rs['msg'] = "问卷名解析错误，检查提交问卷格式！";
                $rs['code'] = 0;
            }
            else{
                switch($httpstr)
                {
                    case 2:
                        $rs['msg'] = "上传成功！";
                        $rs['code'] = 2;
                        $rs['log'] = $httpstr;
                        break;
                    case -1:
                        $rs['msg'] = "问卷名和已有问卷重复！";
                        $rs['code'] = 1;

                        break;
                    default:
                        //暂存文件路径
                        session("tspath", $filepath);
                        $rs['msg'] = "返回内容";
                        $rs['code'] = 1;
                        $rs['log'] = $httpstr;
                        break;
                }
            }

        }
        $this->ajaxReturn($rs);

    }

    public function testsourceSave()
    {
        $filepath = session("tspath");

        $url = "http://localhost:8090/demo/Main";
        $data['action']='savequiz';
        $data['filepath'] = $filepath;
        $data['sourcename'] = I("sourcename");


        $httpstr = http($url, $data, 'GET', array("Content-type: text/html; charset=utf-8"));//调用问卷部分java

        if ($httpstr == 1){
            $rs['msg'] = "保存成功！";
            $rs['code'] = 1;
        }
        else if ($httpstr == -1){
            $rs['msg'] = "问卷重复！";
            $rs['code'] = -1;
        }
        else{
            $rs['msg'] = "发生错误！";
            $rs['code'] = 0;
        }

        $this->ajaxReturn($rs);

    }



}
